<template>
  <div>
    <el-dialog
      :title="isEdit ? '更新资源' : '添加资源'"
      :visible="show"
      width="30%"
      close-on-click-modal
      @close="handleClose"
    >
      <el-form ref="form" :model="resource" label-width="80px">
        <el-form-item label="资源名称">
          <el-input v-model="resource.name"></el-input>
        </el-form-item>
        <el-form-item label="资源路径">
          <el-input v-model="resource.url"></el-input>
        </el-form-item>
        <el-form-item label="资源分类">
          <el-select v-model="resource.categoryId" clearable placeholder="全部">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="resource.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button>取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { createOrUpdateResource } from '@/apis/resource'
export default Vue.extend({
  name: 'create-or-update-resource',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    categoryList: [],
    resource: {
      type: Object,
      default: function() {
        return {
          id: '',
          name: '',
          url: '',
          description: '',
          categoryId: ''
        }
      }
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async onSubmit() {
      const { data } = await createOrUpdateResource(this.resource)
      if (data.code === '000000') {
        this.$message.success(this.isEdit ? '更新资源成功！' : '添加资源成功！')
        this.$emit('changeDialog', false, true)
      }
    },
    handleClose() {
      this.$emit('changeDialog', false)
    }
  }
})
</script>
<style lang="scss" scoped>
</style>
